﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FGHandler
{
    public partial class Form1 : Form
    {
        private FGHandler.LFSHandler _handler;
        public Form1()
        {
            InitializeComponent();
            _handler = new LFSHandler();
            _handler.PlayerJoined += new LFSHandler.PlayerJoinedEventHandler(_handler_PlayerJoined);
            _handler.FGRaceTrackConnectionLeave += new LFSHandler.RaceTrackConnectionLeave(_handler_FGRaceTrackConnectionLeave);
            _handler.PlayerSplitTime += new LFSHandler.PlayerSplitTimeEventHandler(_handler_PlayerSplitTime);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _handler.Connect();

        }

        delegate void OnPlayerSplitTime(LFSHandler sender, lfsplayer player);
        void _handler_PlayerSplitTime(LFSHandler sender, lfsplayer player)
        {
            /*Console.WriteLine(player.PlayerName);
            foreach (FGHandler.Lap lap in player.LapList)
            {
                Console.WriteLine(lap.ToString());
            }*/


            if (listView1.InvokeRequired)
            {
                Invoke(new OnPlayerSplitTime(_handler_PlayerSplitTime), new object[] { sender, player });
                return;
            }
            else
            {
                try
                {
                    if (listView1.Items.ContainsKey(player.Username))
                    {
                        listView1.Items[player.Username].SubItems[0].Text = "Lap: " + player.CurrentLap.ToString();
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        delegate void OnPlayerLeave(LFSHandler sender, lfsplayer player);
        void _handler_FGRaceTrackConnectionLeave(LFSHandler sender, lfsplayer player)
        {
            //Console.WriteLine(player.PlayerName + " left :(");
            if (listView1.InvokeRequired)
            {
                Invoke(new OnPlayerLeave(_handler_FGRaceTrackConnectionLeave), new object[] { sender, player });
                return;
            }
            else
            {
                if (listView1.Items.ContainsKey(player.Username))
                {
                    listView1.Items[player.Username].Remove();
                }
            }
        }

        delegate void OnPlayerJoined(LFSHandler sender, lfsplayer player);
        void _handler_PlayerJoined(LFSHandler sender, lfsplayer player)
        {
            //Console.WriteLine("{0}, {1}, {2}", player.PlayerName, player.PlayerID, player.ConnectionID);


            if (listView1.InvokeRequired)
            {
                Invoke(new OnPlayerJoined(_handler_PlayerJoined), new object[] { sender, player });
                //return;
            }
            else
            {
                ListViewItem lvi = new ListViewItem(player.PlayerName);
                lvi.Name = player.Username;
                lvi.SubItems.Add(player.ConnectionID.ToString());
                listView1.Items.Add(lvi);
                
            }

            /*if (listBox1.InvokeRequired)
            {
                BeginInvoke(new OnPlayerJoined(_handler_PlayerJoined), new object[] { sender, player });
                return;
            }
            else
                listBox1.Items.Add(player.PlayerName + " (" + player.Username + ")");            */
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            MessageBox.Show("Total Racers: " + _handler.TotalRacers);
        }

        
    }
}
